//
//  PanoAnnotationService.swift
//  TTLive
//
//  Created by Toj on 3/8/21.
//

import Foundation
import PanoRtc

open class PanoAnnotationService: NSObject {
    
    private override init() { }
    public static let shared = PanoAnnotationService()

    private var appID = ""
    private var rtcServer = ""
    
    private var token = ""
    private var channelId = ""
    private var userID: UInt64 = 0
    private var annotationId: String = ""
    
    private var panoKit: PanoRtcEngineKit?
    private var annotation: PanoRtcAnnotation?
    private var view: TTView?
    
    private let type: PanoWBToolType = .path
    private let role: PanoWBRoleType = .viewer
    
    private var panoWBColor = PanoWBColor.white
 
    public var didJoined: TTPanoDidJoined?
    
    public func setupServerParameter(appID: String, rtcServer: String) {
        self.appID = appID
        self.rtcServer = rtcServer
    }
    
    public func setupChannelParameter(token: String, channelId: String, userid: UInt64) {
        self.token = token
        self.channelId = channelId
        self.userID = userid
    }
    
    public func joinChannel() {
        setupAnnotationEngine()
        
        joinLiveRoom()
    }
    
    private func setupAnnotationEngine() {
        let config = PanoRtcEngineConfig()
        config.appId = appID
        config.rtcServer = rtcServer
        
        panoKit = PanoRtcEngineKit.engine(with: config, delegate: self)
    }
    
    private func joinLiveRoom() {
        let channelConfig = PanoRtcChannelConfig()
        channelConfig.mode = .channelMeeting
        
        let result = panoKit?.joinChannel(withToken: token, channelId: channelId, userId: userID, config: channelConfig) ?? .failed
        if result != .OK {
            print("加入失败，请根据 PanoResult 进行处理, code:\(result.rawValue)")
            return
        }
    }
    
    public func joinExternalAnnotation(annotationId: String, view: TTView) {
        self.view = view
        
        joinExternalAnnotation(annotationId: annotationId)
    }
        
    public func joinExternalAnnotation(annotationId: String) {
        /*
        let manager = panoKit?.annotationManager
        manager?.setDelegate(self)
        annotation = manager?.externalAnnotation(annotationId)
        annotation?.setToolType(type)
        annotation?.setRoleType(role)
        annotation?.setColor(panoWBColor)
 */
    }
    
    public func startAnnotation(view: TTView) {
        let result = annotation?.start(view) ?? .fatal
        if result != .OK {
            print("start: \(result.rawValue)")
        }
    }
    
    public func stopAnnotation() {
        let result = annotation?.stop() ?? .fatal
        if result != .OK {
            print("stop: \(result.rawValue)")
        }
    }
    
    public func setRoleType(role: Int) {
        let type = PanoWBRoleType(rawValue: role) ?? .viewer
        annotation?.setRoleType(type)
    }
    
    public func quitLiveRoom(complete quitBlock: (()->()?)) {
        quitLiveRoom()
    }
    
    private func quitLiveRoom() {
        annotation?.stop()
        panoKit?.leaveChannel()
        
        appID = ""
        rtcServer = ""
        token = ""
        channelId = ""
        userID = 0
        annotationId = ""
        panoKit = nil
        annotation = nil
        view = nil
        didJoined = nil
    }
}

extension PanoAnnotationService: PanoRtcEngineDelegate {
    public func onChannelJoinConfirm(_ result: PanoResult) {
        didJoined?()
    }
}


extension PanoAnnotationService: PanoRtcAnnotationManagerDelegate {
    
    public func onExternalAnnotationStart(_ annotationId: String) {
        guard let annotationView = view, annotationId != self.annotationId else { return }
        
        startAnnotation(view: annotationView)
    }
    
    public func onExternalAnnotationStop(_ annotationId: String) {
        guard annotationId != self.annotationId else { return }
        
        stopAnnotation()
    }
}
